package com.audaque.springboot.foshanupload.core.util;


import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;

public class ResourceUtil {


    public static String getPathByHutoolResourceUtil(String dir) throws Exception {
        if (StringUtils.isNotBlank(dir)) {
            if (dir.startsWith("/") || dir.startsWith("\\")) {
                throw new Exception("资源路径不合法");
            }else{
                if(dir.contains("classpath:/")){
                    String[] split = dir.split("classpath:/");
                    dir = split[1];

                }
            }

        }
        if(StringUtils.isBlank(dir)){
            throw new Exception("资源路径为空");
        }
        String rootPath = cn.hutool.core.io.resource.ResourceUtil.getResource("").toURI().getPath();///E:/huaAo/code/myDiy/脚手架/单体/foshanupload/web/target/classes/
        URL resource = cn.hutool.core.io.resource.ResourceUtil.getResource(dir);
        if (resource == null) {
            return null;
        }
        return resource.toURI().getPath();

    }


    public static String getPathByGetResourceURLDecoder(String dir) throws Exception {
        if (StringUtils.isNotBlank(dir)) {
            if (dir.startsWith("/") || dir.startsWith("\\")) {
                throw new Exception("资源路径不合法");
            }else{
                if(dir.contains("classpath:/")){
                    String[] split = dir.split("classpath:/");
                    dir = split[1];

                }
            }

        }
        if(StringUtils.isBlank(dir)){
            throw new Exception("资源路径为空");
        }
        String rootPath = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").getPath(), "UTF-8"); ///E:/huaAo/code/myDiy/脚手架/单体/foshanupload/web/target/classes/
        URL resource = Thread.currentThread().getContextClassLoader().getResource(dir);
        if (resource == null) {
            return null;
        }

        return URLDecoder.decode(resource.getPath(), "UTF-8");
    }


    public static String getPathByGetResourceToURI(String dir) throws Exception {
        if (StringUtils.isNotBlank(dir)) {
            if (dir.startsWith("/") || dir.startsWith("\\")) {
                throw new Exception("资源路径不合法");
            }else{
                if(dir.contains("classpath:/")){
                    String[] split = dir.split("classpath:/");
                    dir = split[1];

                }
            }

        }
        if(StringUtils.isBlank(dir)){
            throw new Exception("资源路径为空");
        }
        String rootPath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath(); ///E:/huaAo/code/myDiy/脚手架/单体/foshanupload/web/target/classes/
        URL resource = Thread.currentThread().getContextClassLoader().getResource(dir);
        if (resource == null) {
            return null;
        }
        return resource.toURI().getPath();
    }


    public static String getURLSource(String absoluteUrl) throws Exception {
        File file = new File(absoluteUrl);
        // 通过输入流获取html二进制数据
        InputStream inStream = new FileInputStream(file);
        // 把二进制数据转化为byte字节数据
        byte[] data = readInputStream(inStream);
        String htmlSource = new String(data);
        inStream.close();
        return htmlSource;

    }

    public static byte[] readInputStream(InputStream instream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1204];
        int len = 0;
        while ((len = instream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        instream.close();
        return outStream.toByteArray();
    }


}
